package com.cch.cooperation.api.support.controller;

import cn.hutool.core.util.ObjectUtil;
import com.cch.cooperation.dao.file.dto.FileDTO;
import com.cch.cooperation.api.support.dto.FileTmpDelDTO;
import com.cch.cooperation.api.support.service.FileServiceImpl;
import com.cch.cooperation.common.exception.BizException;
import com.cch.cooperation.common.web.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Api(tags = "文件接口")
@RestController
@RequestMapping("/api/support/file")
public class FileController {

    @Resource
    private FileServiceImpl fileService;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    public Response<FileDTO> upload(MultipartFile file, MultipartFile fileCover, String module) throws IOException {
        if (ObjectUtil.isNull(file) || file.isEmpty()) {
            throw new BizException("上传的文件不能为空");
        }
        InputStream coverInputSteam = ObjectUtil.isNotNull(fileCover) ? fileCover.getInputStream() : null;
        FileDTO data = fileService.upload(module, file.getOriginalFilename(), file.getInputStream(), coverInputSteam);
        return Response.success(data);
    }

    @ApiOperation(value = "文件访问", hidden = true)
    @GetMapping("/{year}/{module}/{fileName}")
    public ResponseEntity file(@PathVariable("year") String year, @PathVariable("module") String module, @PathVariable("fileName") String fileName) throws IOException {
        InputStream inputStream = fileService.file(year, module, fileName);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentLength(inputStream.available());
        headers.setContentType(MediaType.parseMediaType("application/octet-stream"));
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", fileName));
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(inputStream.available())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(inputStream));
    }

    @ApiOperation("临时文件上传")
    @PostMapping("/uploadTmp")
    public Response<FileDTO> uploadTmp(String bizId, MultipartFile file, MultipartFile fileCover, String module) throws IOException {
        if (ObjectUtil.isNull(file) || file.isEmpty()) {
            throw new BizException("上传的文件不能为空");
        }
        InputStream coverInputSteam = ObjectUtil.isNotNull(fileCover) ? fileCover.getInputStream() : null;
        FileDTO data = fileService.uploadTmp(bizId, module, file.getOriginalFilename(), file.getInputStream(), coverInputSteam);
        return Response.success(data);
    }

    @ApiOperation("查询临时文件列表")
    @GetMapping("/queryTmp")
    public Response<List<FileDTO>> queryTmp(@ApiParam("业务id") @RequestParam String bizId) throws IOException {
        List<FileDTO> data = fileService.queryTmp(bizId);
        return Response.success(data);
    }

    @ApiOperation("删除临时文件")
    @GetMapping("/deleteTmp")
    public Response<Void> deleteTmp(@RequestBody FileTmpDelDTO req) throws IOException {
        fileService.deleteTmp(req);
        return Response.success();
    }

}
